

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cup.net.asyn.common &mdash; Cup 3.2.19 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 

  
  <script src="../../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../../index.html" class="icon icon-home"> Cup
          

          
          </a>

          
            
            
              <div class="version">
                3.2.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../cup.html">cup package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">Cup</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
          <li><a href="../../net.html">cup.net</a> &raquo;</li>
        
      <li>cup.net.asyn.common</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cup.net.asyn.common</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Guannan Ma (@mythmgn),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">common function module for cup.net.asyn</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">socket</span>
<span class="kn">import</span> <span class="nn">struct</span>

<span class="kn">from</span> <span class="nn">cup.util</span> <span class="k">import</span> <span class="n">misc</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;ip_port2connaddr&#39;</span><span class="p">,</span> <span class="s1">&#39;add_stub2connaddr&#39;</span><span class="p">,</span> <span class="s1">&#39;add_future2connaddr&#39;</span><span class="p">,</span>
    <span class="s1">&#39;get_ip_and_port_connaddr&#39;</span><span class="p">,</span> <span class="s1">&#39;getip_connaddr&#39;</span><span class="p">,</span> <span class="s1">&#39;getport_connaddr&#39;</span><span class="p">,</span>
    <span class="s1">&#39;getstub_connaddr&#39;</span><span class="p">,</span> <span class="s1">&#39;getfuture_connaddr&#39;</span>
<span class="p">]</span>


<div class="viewcode-block" id="ip_port2connaddr"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.common.ip_port2connaddr">[docs]</a><span class="k">def</span> <span class="nf">ip_port2connaddr</span><span class="p">(</span><span class="n">peer</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    connaddr is a 64bit int</span>
<span class="sd">        32 -  16    - 16   - 32</span>
<span class="sd">        ip - port   - stub - future</span>

<span class="sd">    :param peer:</span>
<span class="sd">        (ipaddr, port)</span>
<span class="sd">    :return:</span>
<span class="sd">        return a connaddr</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">misc</span><span class="o">.</span><span class="n">check_type</span><span class="p">(</span><span class="n">peer</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)</span>
    <span class="n">ipaddr</span><span class="p">,</span> <span class="n">port</span> <span class="o">=</span> <span class="n">peer</span>
    <span class="n">misc</span><span class="o">.</span><span class="n">check_type</span><span class="p">(</span><span class="n">ipaddr</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span>
    <span class="n">packed</span> <span class="o">=</span> <span class="n">socket</span><span class="o">.</span><span class="n">inet_aton</span><span class="p">(</span><span class="n">ipaddr</span><span class="p">)</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s2">&quot;!L&quot;</span><span class="p">,</span> <span class="n">packed</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;&lt;</span> <span class="mi">64</span><span class="p">)</span> <span class="o">|</span> <span class="p">(</span><span class="n">port</span> <span class="o">&lt;&lt;</span> <span class="mi">48</span><span class="p">)</span></div>


<div class="viewcode-block" id="add_stub2connaddr"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.common.add_stub2connaddr">[docs]</a><span class="k">def</span> <span class="nf">add_stub2connaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">,</span> <span class="n">stub</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    add stub into connaddr</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">pack</span> <span class="o">|</span> <span class="p">(</span><span class="n">stub</span> <span class="o">&lt;&lt;</span> <span class="mi">32</span><span class="p">)</span></div>


<div class="viewcode-block" id="add_future2connaddr"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.common.add_future2connaddr">[docs]</a><span class="k">def</span> <span class="nf">add_future2connaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">,</span> <span class="n">future</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    add future into connaddr</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">pack</span> <span class="o">|</span> <span class="n">future</span></div>


<div class="viewcode-block" id="get_ip_and_port_connaddr"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.common.get_ip_and_port_connaddr">[docs]</a><span class="k">def</span> <span class="nf">get_ip_and_port_connaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get (ip, port) from connaddr</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">ipaddr</span> <span class="o">=</span> <span class="n">getip_connaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">)</span>
    <span class="n">port</span> <span class="o">=</span> <span class="n">getport_connaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">)</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">ipaddr</span><span class="p">,</span> <span class="n">port</span><span class="p">)</span></div>


<div class="viewcode-block" id="getip_connaddr"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.common.getip_connaddr">[docs]</a><span class="k">def</span> <span class="nf">getip_connaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get ip from connaddr</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">socket</span><span class="o">.</span><span class="n">inet_ntoa</span><span class="p">(</span><span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;!L&#39;</span><span class="p">,</span> <span class="n">pack</span> <span class="o">&gt;&gt;</span> <span class="mi">64</span><span class="p">))</span></div>


<div class="viewcode-block" id="getport_connaddr"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.common.getport_connaddr">[docs]</a><span class="k">def</span> <span class="nf">getport_connaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get port from connaddr</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">pack</span> <span class="o">&gt;&gt;</span> <span class="mi">48</span><span class="p">)</span> <span class="o">&amp;</span> <span class="mh">0xffff</span></div>


<div class="viewcode-block" id="getstub_connaddr"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.common.getstub_connaddr">[docs]</a><span class="k">def</span> <span class="nf">getstub_connaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get stub from connaddr</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">pack</span> <span class="o">&gt;&gt;</span> <span class="mi">32</span><span class="p">)</span> <span class="o">&amp;</span> <span class="mh">0xffff</span></div>


<div class="viewcode-block" id="getfuture_connaddr"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.common.getfuture_connaddr">[docs]</a><span class="k">def</span> <span class="nf">getfuture_connaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get future from conaddr</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">pack</span><span class="p">)</span> <span class="o">&amp;</span> <span class="mh">0xffff</span></div>

<span class="c1"># vi:set tw=0 ts=4 sw=4 nowrap fdm=indent</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, CUP-DEV.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../../',
            VERSION:'3.2.19',
            LANGUAGE:'en',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../../_static/doctools.js"></script>
      <script type="text/javascript" src="../../../../_static/language_data.js"></script>

  

  <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>